﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;
using System.Web.UI.WebControls;
using MS.InformationManagement.DAL.DocumentCenter;

namespace MS.InformationManagement.BLL.SearchSender
{
    public class SearchSender
    {

        /// <summary>
        /// Create a dinamyc CamlQuery. Max 5 fields
        /// </summary>
        /// <param name="field">List of fields to find</param>
        /// <param name="value">List of values to find</param>
        /// <param name="type">List of types of field to find (Text,Number,.....)</param>
        /// <param name="operatorType">List of types operators caml query (BeginsWith, Eq,Geq .....)</param>
        /// <returns></returns>
        public List<Remitentes> GetSenders(string NIT_Cedula, string Nombres, string Apellidos, string tipo)
        {
            List<Remitentes> foundSenders = new List<Remitentes>();
            SPList ListRemitentes = SPContext.Current.Web.Lists.TryGetList(GlobalConstants.LIST_SENDERS);
            SPListItemCollection collItems = null;
            SPQuery query = new SPQuery();

            #region query Dinamico
            List<string> fields = new List<string>();
            List<string> values = new List<string>();
            List<string> types = new List<string>();
            List<string> operatorTypes = new List<string>();


            if (NIT_Cedula.Length > 0 && !NIT_Cedula.Trim().Equals(string.Empty))
            {
                fields.Add("Remitentes_NIT_Cedula");
                values.Add(NIT_Cedula);
                types.Add("Text");
                operatorTypes.Add("BeginsWith");
            }

            if (Nombres.Length > 0 && !Nombres.Trim().Equals(string.Empty))
            {
                fields.Add("Remitentes_Nombres");
                values.Add(Nombres);
                types.Add("Text");
                operatorTypes.Add("BeginsWith");
            }

            if (Apellidos.Length > 0 && !Apellidos.Trim().Equals(string.Empty))
            {
                fields.Add("Remitentes_Apellidos");
                values.Add(Apellidos);
                types.Add("Text");
                operatorTypes.Add("BeginsWith");
            }

            if (tipo.Length > 0 && !tipo.Trim().Equals(string.Empty))
            {
                fields.Add("Remitentes_Tipo");
                values.Add(tipo);
                types.Add("Text");
                operatorTypes.Add("BeginsWith");
            }

            #endregion

            if (ListRemitentes != null)
            {
                string _cadena = string.Empty;
                query.Query = CamlHelper.CreateCAMLQuery(values.ToArray(), fields.ToArray(), types.ToArray(), operatorTypes.ToArray(), ref _cadena);
                collItems = ListRemitentes.GetItems(query);

                if (collItems.Count > 0)
                {
                    foreach (SPListItem item in collItems)
                    {
                        foundSenders.Add(new Remitentes {
                            NITCedulaRemitente = item[CustomSiteColumns.FIELD_REMITENTES_NIT_CEDULA_NAME] + "",
                            NombresRemitente = item[CustomSiteColumns.FIELD_REMITENTES_NOMBRES_NAME] + "",
                            ApellidosRemitente = item[CustomSiteColumns.FIELD_REMITENTES_APELLIDOS_NAME] + "",
                            Id = item.ID
                        });
                    }
                }
            }
            
            foundSenders = foundSenders.OrderBy(p => p.NombresRemitente).ThenBy(p => p.ApellidosRemitente).ToList();

            return foundSenders;
        }

    }
}
